import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';
import {getMovieListApi} from '../../api/movieApi';

const initialState = {
  list: [],
  total: 0,
};

export const getMovieData = createAsyncThunk('movie/getMovie', async () => {
  return getMovieListApi(0);
});

const movieSlice = createSlice({
  name: 'movie',
  initialState,
  reducers: {},
  extraReducers(builder) {
    builder.addCase(getMovieData.pending, state => {
      console.log('🚀 ~ pending');
    });
    builder.addCase(getMovieData.fulfilled, (state, {payload}) => {
      console.log('🚀 ~ fulfilled', payload);
      state.list = payload.data.datas;
      state.total = payload.data.datas.length;
    });
    builder.addCase(getMovieData.rejected, (state, error) => {
      console.log('🚀 ~ rejected', error);
    });
  },
});

export const {loadDataEnd} = movieSlice.actions;

export default movieSlice.reducer;
